﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Hangjing.SQLServerDAL;
using Hangjing.Model;
using Hangjing.Cache;
using Hangjing.BaiduMapServerAPI;

namespace Hangjing.WebCommon
{
    /// <summary>
    ///配送费计算类
    ///从门店开始,经过其他所有店,再到用户
    /// </summary>
    public class SendFee
    {
        private decimal distance = 0;
        private SendfeeInfo param = null;
        SendInfo send = new SendInfo();

        private int[] use_old_rule_id = { 923, 1065, 926, 920, 1154, 1976 };

        public SendFee(SendfeeInfo info)
        {
            param = info;
        }

        /// <summary>
        /// 计算配送费
        /// </summary>
        /// <returns></returns>
        public SendInfo getSendFee()
        {
            send.Distance = 0;
            send.sendmoney = 0;
            send.ShopID = param.ShopID;

   
            PointsInfo shop = new Points().GetSendFeeModel(param.ShopID);

            if (param.latlng.ulat != "0" && shop.Lat.Length > 0)
            {
                BaiduMap map = new BaiduMap(param.latlng.ulat, param.latlng.ulng, shop.Lat, shop.Lng, shop.NamePy);
                distance = map.getDrivingDistance();
                send.Distance = distance;

                param.cityname = shop.NamePy;
            }


            switch (shop.SN1)
            {
                case "0":
                    send.sendmoney = param.userpay * Convert.ToDecimal((Convert.ToDouble(shop.point) / 100));
                    break;
                case "1":
                    send.sendmoney = Convert.ToInt32(shop.point);
                    break;
                case "2":

                    // if (use_old_rule_id.Contains(shop.Unid)) //旧的规则
                    if (shop.PTimes == 1)
                    {
                        send.sendmoney = carSendMoney();
                    }
                    else
                    {
                        send.sendmoney = countShopSendMoney(shop);
                    }

                    break;
            }

            if (shop.SendLimit > 0)
            {
                send.sendmoney = send.sendmoney * shop.SendLimit;
            }

            send.sendmoney = Convert.ToInt32(send.sendmoney);

            return send;

        }



        /// <summary>
        /// 根据距离计算服务费,每个商家设置不同
        /// 从0公里到5公里，每2公里收2元 ,从5公里开始，每2公里加收10元 
        /// </summary>
        /// <returns></returns>
        public decimal carSendMoney()
        {
            decimal sendmoney = 0;
            shopdeliveryInfo set = null;

            IList<shopdeliveryInfo> list = new shopdelivery().GetListByTogoNum(param.ShopID);
            foreach (var item in list)
            {
                if (item.distancestart <= distance && item.distanceend >= distance)
                {
                    set = item;

                    sendmoney = item.ReveFloat1 + item.sendmoney * distance;

                    break;

                }
            }
            return Convert.ToInt32(sendmoney + 0.0001M);
        }

        public decimal countShopSendMoney(PointsInfo shop)
        {

            send.Distance = 0;
            send.sendmoney = 0;
            distance = 0;

            shopexpressFeeConfigInfo set = new shopexpressFeeConfig().GetModel(param.ShopID);
            if (set == null)
            {
                send.sendmoney = -1;
                return 0;
            }

            if (set.reveint3 == 1)
            {
                BaiduMap map = new BaiduMap(set.Lat, set.Lng, shop.Lat, shop.Lng, param.cityname);
                distance += Convert.ToDecimal(map.getDrivingDistance().ToString("0.0"));
            }

            {
                BaiduMap map = new BaiduMap( param.latlng.ulat, param.latlng.ulng, shop.Lat, shop.Lng, param.cityname);
                distance += Convert.ToDecimal(map.getDrivingDistance().ToString("0.0"));
            }

    

            send.Distance = distance;


            decimal sendmoney = 0;
            if (set == null)
            {
                sendmoney = -1;
                return sendmoney;
            }

            //，1判断距离，2判断时间
            decimal senconddistance = Math.Max(Math.Min((distance - set.basedistance), (set.seconddistance - set.basedistance)), 0);
            decimal thireddistance = Math.Max((distance - set.seconddistance), 0);
            sendmoney = set.basedistanceprice + senconddistance * set.seconddistancePerPrice + thireddistance * set.thirdDistancePerPrice;

            int sendtime = Convert.ToInt32(DateTime.Now.ToShortTimeString().Replace(":", ""));


            int nigone = Convert.ToInt32(set.starttime1.ToShortTimeString().Replace(":", ""));
            int nigtwo = Convert.ToInt32(set.endtime1.Replace(":", ""));

          
            if (nigone >= nigtwo)//说明跨天了。
            {
                if ((sendtime >= nigone && sendtime <= 2400) || (sendtime <= nigtwo))
                {
                    sendmoney += set.reveint1;
                }
            }
            else
            {
                if (sendtime >= nigone && sendtime <= nigtwo)
                {
                    sendmoney += set.reveint1;
                }
            }




            int nigthree = Convert.ToInt32(set.starttime2.ToShortTimeString().Replace(":", ""));
            int nigfour = Convert.ToInt32(set.endtime2.Replace(":", ""));

            if (nigthree >= nigfour)//说明跨天了。
            {
                if ((sendtime >= nigthree && sendtime <= 2400) || (sendtime <= nigfour))
                {
                    sendmoney += set.reveint2;
                }
            }
            else
            {
                if (sendtime >= nigthree && sendtime <= nigfour)
                {
                    sendmoney += set.reveint2;
                }
            }


            sendmoney = Convert.ToInt32(sendmoney + 0.0001M);

            send.sendmoney = sendmoney;

            return sendmoney;

        }


        /// <summary>
        /// 计算跑腿配送费
        /// </summary>
        /// <returns></returns>
        public SendInfo getExpressSendFee()
        {
            SendInfo send = new SendInfo();

            send.Distance = 0;
            send.sendmoney = 0;

            expressFeeConfigInfo set = new expressFeeConfig().GetModel(param.ShopID);
            if (set == null)
            {
                send.sendmoney = -1;
                return send;
            }


            if (set.reveint3 == 1)
            {
                BaiduMap map = new BaiduMap(set.Lat, set.Lng, param.latlng.ulat, param.latlng.ulng, param.cityname);
                distance += Convert.ToDecimal(map.getDrivingDistance().ToString("0.0"));
            }

            {
                BaiduMap map = new BaiduMap(param.latlng.ulat, param.latlng.ulng, param.latlng.slat, param.latlng.slng, param.cityname);
                distance += Convert.ToDecimal(map.getDrivingDistance().ToString("0.0"));
            }




            send.Distance = distance;
            send.sendmoney = Convert.ToInt32(carExpressSendMoney(set));
            return send;
        }


        /// <summary>
        /// 根据距离计算服务费,每个商家设置不同
        /// 从0公里到5公里，收2元 ,从5公里开始，每公里加收10元 
        /// </summary>
        /// <returns></returns>
        public decimal carExpressSendMoney(expressFeeConfigInfo set)
        {
            decimal sendmoney = 0;
            if (set == null)
            {
                sendmoney = -1;
                return sendmoney;
            }

            //，1判断距离，2判断时间
            decimal senconddistance = Math.Max(Math.Min((distance - set.basedistance), (set.seconddistance - set.basedistance)), 0);
            decimal thireddistance = Math.Max((distance - set.seconddistance), 0);
            sendmoney = set.basedistanceprice + senconddistance * set.seconddistancePerPrice + thireddistance * set.thirdDistancePerPrice;

            int sendtime = Convert.ToInt32(param.sendtime.ToString("HHmm").Replace(":", ""));


            int nigone = Convert.ToInt32(set.starttime1.ToString("HHmm").Replace(":", ""));
            int nigtwo = Convert.ToInt32((Convert.ToDateTime(set.endtime1)).ToString("HHmm").Replace(":", ""));

            if (nigone >= nigtwo)//说明跨天了。
            {
                if ((sendtime >= nigone && sendtime <= 2400) || (sendtime <= nigtwo))
                {
                    sendmoney += set.reveint1;
                }
            }
            else
            {
                if (sendtime >= nigone && sendtime <= nigtwo)
                {
                    sendmoney += set.reveint1;
                }
            }


            int nigthree = Convert.ToInt32(set.starttime2.ToString("HHmm").Replace(":", ""));
            int nigfour = Convert.ToInt32((Convert.ToDateTime(set.endtime2)).ToString("HHmm").Replace(":", ""));

            if (nigthree >= nigfour)//说明跨天了。
            {
                if ((sendtime >= nigthree && sendtime <= 2400) || (sendtime <= nigfour))
                {
                    sendmoney += set.reveint2;
                }
            }
            else
            {
                if (sendtime >= nigthree && sendtime <= nigfour)
                {
                    sendmoney += set.reveint2;
                }
            }

            return sendmoney;
        }

    }
}